package org.jimmy.tang.methodinterceptor;

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;

/**
 * @author jimmy
 * 
 */
public class ProfilingExample {
    public static void main(String[] args) {
        WorkerBean bean = getWorkerBean();
        bean.doSomeWork1(10000000);
        bean.doSomeWork2(10000000);
    }

    private static WorkerBean getWorkerBean() {

        // 1. create a Pointcut
        JdkRegexpMethodPointcut pc = new JdkRegexpMethodPointcut();
        pc.setPattern(".*methodinterceptor.*");

        // AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
        // pc.setExpression("execution(* aoptest.WorkerBean.doSomeWork*(..))");

        // 2. create a Advice
        ProfilingAdvice advice = new ProfilingAdvice();

        // 3. create a Advisor(Aspect)
        Advisor advisor = new DefaultPointcutAdvisor(pc, advice);

        // 4. create a Target
        WorkerBean target = new WorkerBean();

        // 5. create a ProxyFactory to Weaving
        ProxyFactory factory = new ProxyFactory();
        factory.setTarget(target);
        factory.addAdvisor(advisor);

        return (WorkerBean) factory.getProxy();
    }
}
